<template>
  <div class="pad-20 circle-2-tab">
    <!-- status-0 待发布; status-1 待开始; status-2 进行中;  status-3 已结束; -->
    <div class="status-bar status-0" v-if="status == '1'">待发布</div>
    <div class="status-bar status-0" v-if="status == '2'">待开始</div>
    <div class="status-bar status-0" v-if="status == '3'">进行中</div>
    <div class="status-bar status-0" v-if="status == '4'">已结束</div>
    <div class="active-main pad-t-18 pad-b-18 mar-t-10 position-r">
     <!-- <div v-if="promotionStatus != 1" class="layer-over"></div>-->
      <el-row :gutter="20">
        <el-col :span="7" class="flex-center">
          <promotion-phone ref="promotionPhone"></promotion-phone>
        </el-col>
        <el-col :span="17">
          <div class="active-r mar-r-20">
            <h2>商品活动设置</h2>
            <el-form class="form-diy goods-form">
              <el-form-item label="活动标题" label-width="80px" class="none-mar-b pad-b-10 pad-t-20">
                <div class="mar-l-12 mar-r-12">
                  <el-input placeholder="标题最多4个字" maxlength="4" style="width: 240px" v-model="form.title"
                            @change="changePromotionInfo"/>
                </div>
              </el-form-item>
              <el-form-item label="列表样式" label-width="80px" class="none-mar-b pad-b-10">
                <div class="flex v-align-c font-12 pad-r-30 pad-l-10 pad-t-10">
                  <el-radio-group v-model="form.styleType" @change="changePromotionInfo">
                    <el-radio :label="1">单列大图</el-radio>
                    <el-radio :label="2">双列并排</el-radio>
                    <el-radio :label="3">单列横排</el-radio>
                    <el-radio :label="4">阶梯横排</el-radio>
                  </el-radio-group>
                </div>
              </el-form-item>
              <el-form-item label="背景颜色" label-width="80px" class="none-mar-b pad-b-15">
                <div class="flex font-12 pad-r-30">
                  <el-color-picker v-model="form.backgroundColor" class="mar-l-12"
                                   @change="changePromotionInfo"></el-color-picker>
                </div>
              </el-form-item>
              <el-form-item label="选择商品" label-width="80px" class="none-mar-b pad-b-10">
                <div class="flex v-align-c font-12 pad-r-30 pad-l-10 pad-t-10">
                  <el-radio v-model="goodsType" label="0">手动选择</el-radio>
                </div>
              </el-form-item>
              <el-form-item class="none-mar-b pad-b-10">
                <div class="flex v-align-c font-12 pad-l-12">
                  <el-button size="small" class="long-btn" @click="addGoods" type="primary" plain>+添加商品</el-button>
                </div>
              </el-form-item>
            </el-form>
            <div class="select-list mar-l-12 mar-r-12 mar-t--2">
              <div class="tips">温馨提示：序号数字越小排在越前面</div>
              <!-- 内容列表 -->
              <promotion-item-list ref="promotionItemList"></promotion-item-list>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <!-- 添加商品弹窗 -->
    <promotion-item-add ref="promotionItemAdd"></promotion-item-add>
    <!-- 设置商品活动价弹窗 -->
    <promotion-sku-price ref="promotionSkuPrice"></promotion-sku-price>
    <div class="height-86"></div>
    <div class="footer-bar flex-center v-align-c bg-color-white pad-t-20 pad-b-20 pad-r-20">
      <el-button type="default" class="mar-l-20 medium-s-btn mar-l-10 mar-r-10" @click="goPageList">返回上一级</el-button>
      <el-button type="primary" class="mar-l-20 medium-s-btn mar-l-10 mar-r-10" @click="savePromotion(promotionStatus)" v-if="promotionStatus == 1 || promotionStatus == 2">仅保存
      </el-button>
      <el-button type="primary" class="mar-l-20 medium-s-btn mar-l-10 mar-r-10" @click="savePublish" v-if="promotionStatus == 1">保存并发布</el-button>
    </div>

    <el-dialog title="设置活动开始时间" :visible.sync="dialogFormVisible" :close-on-press-escape="true" width="650px">
      <el-form>
        <el-form-item label="活动时间：" label-width="170px" prop="taxAfterFee">
          <el-date-picker
            v-model="datePicker"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="button_ok">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
  import promotionItemList from '../../components/promotion-item-list'
  import promotionItemAdd from '../../components/promotion-item-add'
  import promotionSkuPrice from '../../components/promotion-sku-price'
  import promotionPhone from '../../components/promotion-phone'
  import { savePromotion, getPromotionInfo } from '@/api/sell/promotion'
  import elDragDialog from '@/directive/el-dragDialog' // base on element-ui

  export default {
    components: { promotionItemList, promotionItemAdd, promotionSkuPrice, promotionPhone },
    directives: {
      // 拖动窗口
      elDragDialog
    },
    data() {
      return {
        form: {
          title: '',
          backgroundColor: '',
          styleType: 1
        },
        status: '',
        promotionStatus: '1',
        datePicker: [],
        dialogFormVisible: false,
        promotionId: '',
        goodsType: '0'
      }
    },
    // 初始化页面
    created() {
      const queryParams = this.getCurrentPage().query
      const promotionId = queryParams.promotionId
      if (promotionId) {
        this.promotionId = promotionId
        this.getPromotionInfo()
      }
    },
    mounted() {
    },
    filters: {},
    methods: {
      addGoods() {
        this.$refs.promotionItemAdd.dialogFormVisible = true
        this.$refs.promotionItemAdd.isSelectDataList()
      },
      changePromotionInfo() {
        this.$refs.promotionPhone.styleType = this.form.styleType
        this.$refs.promotionPhone.title = this.form.title
        this.$refs.promotionPhone.backgroundColor = this.form.backgroundColor
      },
      getPromotionInfo() {
        const that = this
        const data = {
          promotionId: this.promotionId
        }
        getPromotionInfo(data).then((response) => {
          if (response.isError) return
          that.form.title = response.data.title
          that.promotionStatus = response.data.promotionStatus
          that.status = response.data.status
          that.form.backgroundColor = this.$utils.isExist(response.data.backgroundColor) ? response.data.backgroundColor : ''
          that.form.styleType = response.data.styleType
          if (response.data.startTime && response.data.endTime) {
            that.datePicker = [response.data.startTime, response.data.endTime]
          }
          that.$bus.$emit('promotion-update', response.data)
          that.changePromotionInfo()
        })
      },
      savePublish() {
        this.dialogFormVisible = true
      },
      button_ok() {
        const datePicker = this.datePicker
        if (datePicker == null || datePicker.length === 0) {
          this.$message.error('请选择日期!')
          return
        }
        this.savePromotion('2')
      },
      goPageList() {
        this.$router.push('/goods/promotion/list/index')
      },
      savePromotion(promotionStatus) {
        const title = this.form.title
        const backgroundColor = this.$utils.isExist(this.form.backgroundColor) ? this.form.backgroundColor : ''
        const styleType = this.form.styleType
        const promotionId = this.promotionId
        const promotionGoodsList = this.$refs.promotionItemList.dataList
        if (promotionGoodsList == null || promotionGoodsList.length === 0) {
          this.$message.error('请选择活动商品!')
          return
        }
        if (promotionGoodsList.length >= 20) {
          this.$message.error('活动商品不能超过20个!')
          return
        }
        var patt1 = new RegExp(/^([1-9]\d*(\.\d*[1-9][0-9])?)|(0\.\d*[1-9][0-9])|(0\.\d*[1-9])$/)
        var patt2 = new RegExp(/^([0-9]\d*(\.\d*[1-9][0-9])?)|(0\.\d*[1-9][0-9])|(0\.\d*[1-9])$/)
        for (let i = 0; i < promotionGoodsList.length; i++) {
          const goods = promotionGoodsList[i]
          const skuFlag = goods.skuFlag
          if (skuFlag === 1) {
            const promotionGoodsSkuList = goods.promotionGoodsSkuList
            if (promotionGoodsSkuList == null || promotionGoodsSkuList.length === 0) {
              this.$message.error('部分数据异常，请前往设置!')
              return
            }
            for (let j = 0; j < promotionGoodsList.length; j++) {
              const promotionPrice = promotionGoodsList[j].promotionPrice
              if (!this.$utils.isExist(promotionPrice)) {
                this.$message.error('部分商品没有设置活动价格,请前去设置!')
                return
              }

              const fee = (promotionPrice * 1).toFixed(2)
              if (!patt1.test(fee) || fee < 0) {
                this.$message.error('请正确输入商品价格!')
                return
              }
              const distributeIncome = promotionGoodsList[j].distributeIncome
              if (!patt2.test(distributeIncome) || distributeIncome < 0) {
                this.$message.error('请输入正确分销佣金!')
                return
              }
              const shareMemberIncome = promotionGoodsList[j].shareMemberIncome
              if (!patt2.test(shareMemberIncome) || shareMemberIncome < 0) {
                this.$message.error('请输入正确的顾客分销商佣金!')
                return
              }
            }
          } else {
            const promotionPrice = goods.promotionPrice
            if (!this.$utils.isExist(promotionPrice)) {
              this.$message.error('部分商品没有设置活动价格,请前去设置!')
              return
            }
            const fee = (promotionPrice * 1).toFixed(2)
            if (!patt1.test(fee)) {
              this.$message.error('请正确输入商品价格!')
              return
            }
            const distributeIncome = goods.distributeIncome
            if (!patt2.test(distributeIncome) || distributeIncome < 0) {
              this.$message.error('请输入正确分销佣金!')
              return
            }
            const shareMemberIncome = goods.shareMemberIncome
            if (!patt2.test(shareMemberIncome) || shareMemberIncome < 0) {
              this.$message.error('请输入正确的顾客分销商佣金!')
              return
            }
          }
        }
        const datePicker = this.datePicker
        let startTime = null
        let endTime = null
        if (promotionStatus * 1 === 2) {
          startTime = datePicker[0]
          endTime = datePicker[1]
        }
        const data = {
          title: title,
          backgroundColor: backgroundColor,
          styleType: styleType,
          userType: '1',
          userId: this.getEnterpriseId(),
          promotionId: promotionId,
          promotionStatus: promotionStatus,
          startTime: startTime,
          endTime: endTime,
          promotionGoodsList: promotionGoodsList
        }
        savePromotion(data).then((response) => {
          if (response.isError) return
          this.$message({ type: 'success', message: '保存成功!' })
          this.$router.push('/goods/promotion/list/index')
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .layer-over{
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
  }
  // 页面总体布局
  .active-main {
    background: #f8f8f8;

    &:after {
      content: '';
      display: block;
      clear: both;
      font-size: 0;
    }

    .active-l {
      .main-content {
        padding-right: 4px;
        padding-left: 2px;
        height: 570px;
        overflow-x: hidden;
        overflow-y: auto;

        &::-webkit-scrollbar {
          width: 4px;
        }

        &::-webkit-scrollbar-thumb {
          border-radius: 10px;
          box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          background: rgba(0, 0, 0, 0.2);
        }

        &::-webkit-scrollbar-track {
          box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
          border-radius: 0;
          background: rgba(0, 0, 0, 0.1);
        }

        h2 {
          margin: 0;
          font-size: 18px;
        }

        ul {
          padding: 0;
          margin: 0;

          &:after {
            content: '';
            display: block;
            clear: both;
            font-size: 0;
          }

          &.show-type-1 {
            li {
              .gooods-pic {
                img {
                  width: 100%;
                  height: 149px;
                  border-top-left-radius: 4px;
                  border-top-right-radius: 4px;
                }
              }
            }
          }

          &.show-type-2 {
            li {
              width: 48%;
              float: left;

              &:nth-child(odd) {
                margin-right: 4%;
              }

              .gooods-pic {
                img {
                  width: 100%;
                  height: 133px;
                  border-top-left-radius: 4px;
                  border-top-right-radius: 4px;
                }
              }

              .buy-now-type {
                display: none;
              }

              .price {
                label {
                  display: none;
                }
              }
            }
          }

          &.show-type-3 {
            li {
              display: -webkit-box;
              display: -webkit-flex;
              display: flex;
              flex-flow: row;
              align-items: center;
              padding: 0 5px 0 8px;

              .pic-box {
                margin-right: 6px;

                .gooods-pic {
                  img {
                    width: 82px;
                    height: 82px;
                  }
                }
              }

              .detail-box {
                -webkit-box-flex: 1;
                -webkit-flex: 1;
                flex: 1;
              }

              .tag {
                span {
                  padding: 1px 5px !important;
                }

                label {
                  padding: 1px 5px !important;
                }
              }

              .price {
                label {
                  display: none;
                }
              }

              .stock {
                display: none;
              }
            }
          }

          &.show-type-4 {
            li {
              display: -webkit-box;
              display: -webkit-flex;
              display: flex;
              flex-flow: row;
              align-items: center;
              padding: 8px 5px 8px 8px;

              &:nth-child(even) {
                .pic-box {
                  &.even {
                    display: none;
                  }

                  &.odd {
                    display: block;
                  }
                }
              }

              &:nth-child(odd) {
                .pic-box {
                  &.even {
                    display: block;
                  }

                  &.odd {
                    display: none;
                  }
                }
              }

              .pic-box {
                margin-right: 6px;

                .gooods-pic {
                  img {
                    width: 122px;
                    height: 122px;
                  }
                }
              }

              .detail-box {
                -webkit-box-flex: 1;
                -webkit-flex: 1;
                flex: 1;
                padding: 0 6px !important;
              }

              .desc {
                margin-top: -6px;
              }

              .tag {
                margin-top: -1px;
                margin-bottom: 0px;

                span {
                  padding: 1px 5px !important;
                }

                label {
                  padding: 1px 5px !important;
                }
              }

              .price {
                .flex-item {
                  > label {
                    display: -webkit-box;
                    display: -webkit-flex;
                    display: flex;
                    margin-top: -8px;
                  }
                }
              }

              .stock {
                display: none;
              }

              .info-bar {
                .buy-now-type {
                  display: none !important;
                }
              }

              .last-item {
                display: block !important;
                margin-top: 5px;
              }
            }
          }

          li {
            list-style-type: none;
            border-radius: 4px;
            box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
            margin-bottom: 15px;

            &:last-child {
              margin-bottom: 0;
            }

            .pic-box {
              &.odd {
                display: none;
              }
            }

            .gooods-pic {
              img {
                display: block;
              }
            }

            .desc {
              height: 36px;
              line-height: 1.5;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 2;
              -webkit-box-orient: vertical;
              word-break: break-word;
            }

            .last-item {
              display: none;
            }

            .info-bar {
              .tag {
                line-height: 1.3;

                span {
                  background: #fed807;
                  color: #333;
                  padding: 2px 5px;
                }

                label {
                  background: #272727;
                  color: #fff;
                  padding: 2px 5px;
                  min-width: 60px;
                  box-sizing: border-box;

                  i {
                    color: #ffc81b;
                    font-style: normal;
                  }
                }
              }

              .price {
                span {
                  color: #FF4949;
                }

                label {
                  text-decoration: line-through;
                }
              }

              .stock {
                color: #6e6e6e;
              }

              label {
                font-weight: normal;
              }
            }
          }
        }

        .picture-default {
          background: #fe8096;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }

    .active-r {
      border: 1px solid #ddd;
      border-radius: 10px;
      background: #fff;
      padding: 20px;
      min-height: 609px;

      h2 {
        font-size: 18px;
        margin: 0;
      }

      .tips {
        color: #409eff;
      }

      .select-table {
        min-height: auto !important;
      }
    }
  }

  .category-style {
    color: #6290ff;
    font-size: 13px;
  }

  .title-line-2 {
    height: 40px;
    line-height: 1.4;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    word-break: break-word;
  }

  // 商品弹窗样式
  .goods-layer {
    h3 {
      margin: -10px 0 0 0;
      color: #303133;
      font-size: 18px;
      text-align: center;
    }

    .dialog-footer {
      position: absolute;
      left: 0;
      bottom: 20px;
      width: 100%;
    }

    .table-container {
      height: 500px;
      overflow-x: hidden;
      overflow-y: auto;
    }
  }

  // 左侧手机模型样式
  .diy-bg-main {
    .diy-phone {
      float: left;
      width: 340px;
      border: 1px solid #ddd;
      border-radius: 10px;
      background: #fff;
      padding: 10px;
      overflow: hidden;

      &.none-border {
        border: none;
        background: none;
        padding: 0px 10px;
        border-radius: 0;
      }
    }

    .diy-phone .phone-head {
      height: 44px;
      background: #fff;
      position: relative;
    }

    .diy-phone .phone-head:before {
      content: "";
      position: absolute;
      top: 15px;
      left: 112px;
      height: 8px;
      width: 8px;
      background: #ddd;
      border-radius: 8px;
    }

    .diy-phone .phone-head:after {
      content: "";
      position: absolute;
      top: 15px;
      left: 128px;
      height: 8px;
      width: 50px;
      background: #ddd;
      border-radius: 8px;
    }

    .diy-phone .phone-title {
      height: 17px;
      background: url(../../../../assets/images/wx-top-2.png) 0 0 no-repeat;
      background-size: 100% auto;
      padding: 0 50px;
      font-size: 15px;
      color: #333333;
      text-align: center;
      line-height: 68px;
      cursor: default;
      overflow: hidden;
      display: block;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
</style>
